package com.fenghongzhang.day003;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void send(View view) {

        Notification.Builder builder = new Notification.Builder(this);

        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("通知头");
        builder.setContentText("内容");
        builder.setWhen(System.currentTimeMillis());

        //阅读以后,自动消息
        builder.setAutoCancel(true);

        //让通知弹出来
        builder.setPriority(Notification.PRIORITY_MAX);
        //加上震动,灯光,声音
        builder.setDefaults(Notification.DEFAULT_ALL);

        // intent 页面中跳转问题.PendingIntent
        Intent intent = new Intent(this, Main2Activity.class);
        //去往哪里就get哪里
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);

        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));


        Notification build = builder.build();

        //通知管理者
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //发送通知
        //不同id代表发送,相同id代表更新
        manager.notify(1,build);

    }

    public void send2(View view) {
        Notification.Builder builder = new Notification.Builder(this);

        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("通知头");
        builder.setContentText("内容");


        //是在系统的通知栏中,执行的.所以要远程访问我们的布局文件.
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);

        //加载一个布局文件
        builder.setCustomContentView(remoteViews);


        Notification build = builder.build();

        //通知管理者
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //发送通知
        //不同id代表发送,相同id代表更新
        manager.notify(1,build);
    }

    public void send3(View view) {
        Notification.Builder builder = new Notification.Builder(this);

        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("通知头");
        builder.setContentText("内容");


        //是在系统的通知栏中,执行的.所以要远程访问我们的布局文件.
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);


        //更改通知的内容
        remoteViews.setTextViewText(R.id.notify_tv,"哈哈");
        remoteViews.setImageViewResource(R.id.notif_img,R.mipmap.ic_launcher_round);

        //加载一个布局文件
        builder.setCustomContentView(remoteViews);


        Notification build = builder.build();

        //通知管理者
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //发送通知
        //不同id代表发送,相同id代表更新
        manager.notify(1,build);
    }

    public void send4(View view) {

        //通知管理者
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("通知头");
        builder.setContentText("内容");
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                index+=10;
                builder.setProgress(100,index,false);
                manager.notify(1,builder.build());
                if(index >=100){
                    builder.setContentText("正在安装...");
                    builder.setProgress(100,index,true);
                    manager.notify(1,builder.build());
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    manager.cancel(1);
                    timer.cancel();
                }
            }
        },0,1000);
        Notification build = builder.build();
        //发送通知
        //不同id代表发送,相同id代表更新
        manager.notify(1,build);

    }

    public void send5(View view) {
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("通知头");
        builder.setContentText("内容");


//        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
//        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));


        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
        inboxStyle.addLine("哈哈哈");
        inboxStyle.addLine("哈哈哈2");
        inboxStyle.addLine("哈哈哈3");

        builder.setStyle(inboxStyle);

//        builder.setVisibility(Notification.VISIBILITY_PUBLIC);

        builder.setPriority(Notification.PRIORITY_MAX);
        builder.setDefaults(Notification.DEFAULT_ALL);


        final Notification build = builder.build();
        //发送通知
        //不同id代表发送,相同id代表更新
        manager.notify(1, build);

    }

    public void send6(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder mBuilder0 = new Notification.Builder(this);
        //消息分组属性,group相同才能分到一组
        mBuilder0.setGroup("2");
        mBuilder0.setSmallIcon(R.mipmap.ic_launcher);
        //被设置setGroupSummary为true的消息会隐藏内容,其内容会显示另外分组成员信息.
//        mBuilder0.setGroupSummary(true);
        mBuilder0.setContentText("11111111");
        mBuilder0.setContentTitle("222222222");


        Notification.Builder mBuilder1 = new Notification.Builder(this);
        mBuilder1.setGroup("2");
        mBuilder1.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder1.setGroupSummary(true);
        mBuilder1.setContentText("333333");
        mBuilder1.setContentTitle("44444444");

        Notification.Builder mBuilder2 = new Notification.Builder(this);
        mBuilder2.setGroup("2");
        mBuilder2.setSmallIcon(R.mipmap.ic_launcher);
//        mBuilder2.setGroupSummary(true);
        mBuilder2.setContentText("55555555555555555");
        mBuilder2.setContentTitle("66666666666");


        Notification.Builder mBuilder3 = new Notification.Builder(this);
        mBuilder3.setGroup("2");
        mBuilder3.setSmallIcon(R.mipmap.ic_launcher);
//        mBuilder3.setGroupSummary(true);
        mBuilder3.setContentText("77777");
        mBuilder3.setContentTitle("8888888");


        manager.notify(0,mBuilder0.build());
        manager.notify(1,mBuilder1.build());
        manager.notify(2,mBuilder2.build());
        manager.notify(3,mBuilder3.build());
    }
}
